/***********************************************************************
 * Module:  DepartmentDto.java
 * Author:  Vanil
 * Purpose: Defines the Class DepartmentDto
 ***********************************************************************/
package hosp;
import db.dao.DepartmentDao;
import db.dao.DoctorDao;
import db.dao.NurseDao;
import db.dao.ProcedurecabinetDao;
import db.dto.DepartmentDto;
import db.dto.DoctorDto;
import db.dto.NurseDto;
import db.dto.ProcedurecabinetDto;
import db.exceptions.*;
import db.jdbc.DepartmentDaoImpl;
import db.jdbc.DoctorDaoImpl;
import db.jdbc.NurseDaoImpl;
import db.jdbc.ProcedurecabinetDaoImpl;
import personal.Doctor;
import personal.Nurse;
import org.simpleframework.xml.Root;

import java.util.ArrayList;
import java.util.Collection;

@Root
public class Department {
    DepartmentDto dto;
    public void setIdDepartment(int idDepartment) {
    dto.setIdDepartment(idDepartment);
    }
    public void setName(String name) throws ProcedurecabinetDaoException, DepartmentDaoException {
        dto.setName(name);
        updateUpload();
    }

    public int getIdDepartment() {

        return dto.getIdDepartment();
    }

    public String getName() {
        return dto.getName();
    }

    public Department(DepartmentDto dto) {
        this.dto = dto;
        
    }
    private void updateUpload() throws ProcedurecabinetDaoException, DepartmentDaoException {
        DepartmentDao dao = new DepartmentDaoImpl();
        dao.update(dto.getIdDepartment(),dto);
    }
    /** @param doctor
    */
   public void addDoctor(Doctor doctor) throws PersonDaoException {
//        this.doctor.add(doctor);
//        doctor.setDepartment(this);
   }
   
   

   public java.util.Collection<Doctor> getDoctors() throws DoctorDaoException {
       Collection<Doctor> docList = new ArrayList<Doctor>();
       DoctorDao dao= new DoctorDaoImpl();
       DoctorDto[] doctorDtos = dao.findByDepartment(this.dto.getIdDepartment());
       for(DoctorDto docDto:doctorDtos)
           docList.add(new Doctor(docDto));
       return docList;
   }
   /**
     * @param oldDoctor */
   public void removeDoctor(Doctor oldDoctor) throws PersonDaoException {
       DoctorDao dao= new DoctorDaoImpl();
       dao.delete(oldDoctor.getIdPerson());
   }


   public java.util.Collection<Nurse> getNurses() throws NurseDaoException {
       Collection<Nurse> docList = new ArrayList<Nurse>();
       NurseDao dao= new NurseDaoImpl();
       NurseDto[] NurseDtos = dao.findByDepartment(this.dto.getIdDepartment());
       for(NurseDto docDto:NurseDtos)
           docList.add(new Nurse(docDto));
       return docList;
   }
   
   /**
     * @param newNurse */
   public void addNurse(Nurse newNurse) throws NurseDaoException, PersonDaoException {
       NurseDao dao = new NurseDaoImpl();
       NurseDto nurseDto = dao.findByPrimaryKey(newNurse.getIdPerson());
       if(nurseDto != null)
       {
           newNurse.setDepartment(this);
       }
       else
       {
           NurseDto dtoNurse = newNurse.getDto();
           dtoNurse.setIdDepartment(this.getIdDepartment());
           dao.insert(dtoNurse);
       }

   }
   /**
     * @param oldNurse */
   public void removeNurse(Nurse oldNurse) throws NurseDaoException {
       NurseDao dao= new NurseDaoImpl();
       dao.delete(oldNurse.getIdPerson());
   }
   

   public java.util.Collection<ProcedureCabinet> getProcedureCabinets() throws ProcedurecabinetDaoException {
      Collection<ProcedureCabinet> docList = new ArrayList<ProcedureCabinet>();
       ProcedurecabinetDao dao= new ProcedurecabinetDaoImpl();
       ProcedurecabinetDto[] ProcedurecabinetDtos = dao.findByDepartment(this.dto.getIdDepartment());
       for(ProcedurecabinetDto docDto:ProcedurecabinetDtos)
           docList.add(new ProcedureCabinet(docDto));
       return docList;
   }

   /**
     * @param newProcedureCabinet */
   public void addProcedureCabinet(ProcedureCabinet newProcedureCabinet) throws ProcedurecabinetDaoException {
       ProcedurecabinetDaoImpl dao = new ProcedurecabinetDaoImpl();
       ProcedurecabinetDto ProcedureCabinetDto = dao.findByPrimaryKey(newProcedureCabinet.getDto().getNumber());
       if(ProcedureCabinetDto != null)
       {
           newProcedureCabinet.setDepartment(this);
       }
       else
       {
           ProcedurecabinetDto dtoProcedureCabinet = newProcedureCabinet.getDto();
           dtoProcedureCabinet.setIdDepartment(this.getIdDepartment());
           dao.insert(dtoProcedureCabinet);
       }  
   }
   
   /**
     * @param oldProcedureCabinet */
   public void removeProcedureCabinet(ProcedurecabinetDto oldProcedureCabinet) throws ProcedurecabinetDaoException {
             ProcedurecabinetDao dao= new ProcedurecabinetDaoImpl();
       dao.delete(oldProcedureCabinet.getNumber());  
   }


}